package cn.buk.tms.eotms.entity;

import com.fasterxml.jackson.annotation.JsonFormat;

import jakarta.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 已出票订单中票号包含的信息
 * @author william
 *
 */
@NamedQueries({
				@NamedQuery(
								name = "BillFlightTicket.searchMonthlyTickets",
								query = "select new cn.buk.tms.eotms.report.dto.ReportFlightTicketDto(o.balanceCode, o.ticketNo, " +
												"o.etdzDate, o.passengerName, o.routeDetail, " +
												"o.parvalue, o.tax, o.serviceCharge, o.discount, o.accountRecv, " +
												"b.op1, b.opDate, b.id, b.bePaid) " +
												"from BillFlightTicket o left join BillInfo b on (o.billNum = b.billNum and o.enterpriseId = b.enterpriseId) " +
												" where o.enterpriseId = :enterpriseId and o.customerId = :customerId and b.opDate >= :beginDate and b.opDate <= :endDate " +
												" order by o.etdzDate, o.balanceCode, o.ticketNo"
				)
})
@Entity
@Table(name="t202", uniqueConstraints=@UniqueConstraint(name="uk_t202_enterprise_id_ticketno", columnNames={"enterprise_id", "balanceCode", "tickNum"}))
public class BillFlightTicket {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private int id;

	@Column(name = "enterprise_id", updatable = false)
	private int enterpriseId;

	/**
	 * 客票所属结算航空公司
	 */
	@Column(length = 3)
	private String balanceCode;

	/**
	 * 票号允许到13位，接受联程票号
	 * 例如：6311712103-04
	 * 自动开票会回填如上格式，在机票订单回填票号时保存到一起了
	 */
	@Column(name = "tickNum", length = 13)
	private String ticketNo;

	/**
	 * 票号数量（联程客票）
	 */
	@Column(name = "ticket_count")
	private Integer ticketCount;

	/**
	 * 截止票号
	 */
	@Column(name = "ticket_no_end", length = 10)
	private String ticketNoEnd;


	/**
	 * 客票类别，用于识别客票本身是中性票，还是航空公司票
	 */
	@Column(name = "tickClass", length = 3)
	private String ticketClass = "000";

	/**
	 * 账单号
	 **/
	@Column(length = 32, nullable = false)
	private String billNum;

	@Column(name = "psgename", length = 50)
	private String passengerName;

	/**
	 * 冗余字段，应该和Order.SaleDate值保持一致
	 */
	@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
	@Temporal(TemporalType.DATE)
	@Column(name = "selldate", insertable = false)
	private Date saleDate;


	/**
	 * 订单日期：订单的生成日期（配合做结算单报表用）
	 */
	@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
	@Temporal(TemporalType.DATE)
	@Column(name = "order_date")
	private Date orderDate;

	/**
	 * 冗余字段，应该和Order.customer值保持一致
	 */
	@ManyToOne
	@JoinColumn(name = "customer_id", foreignKey = @ForeignKey(name = "fk_t202_customer"))
	private Customer customer;

	@Column(name = "customer_id", insertable = false, updatable = false)
	private Integer customerId;


	@Column(name = "accntRcv")
	private double accountRecv;


	@Column(name = "pnr", length = 10)
	private String pnrNo;

	@Column(length = 20)
	private String remark;

	/**
	 * 付款备注
	 */
	@Column(name = "pay_memo")
	private String payMemo;

	//价格相关的字段  来自以前的t204表格

	/**
	 * 公布运价
	 */
	private double parvalue;

	/**
	 * 实际销售运价
	 */
	private double price;


	private double tax;

	/**
	 * 机场税
	 */
	private double airportTax;

	/**
	 * 燃油附加费
	 */
	private double fuelSurcharge;

	@Column(name = "custCommRate")
	private double customerCommissionRate;

	private double discount;

	private double discount2;

	private double hcharge;

	/**
	 * 服务费
	 */
	private double serviceCharge;

	private double lowFare;

	/**
	 * 行程，冗余字段
	 */
	@Column(name = "routeFlight")
	private String routeFlightNo;

	/**
	 * 第一段的起飞日期
	 */
	@Column(name = "routeDDate")
	private String routeDate;

	@Column(name = "segInfo")
	private String routeSegInfo;

	private String routeClass;

	private String routeDetail;

	@Column(length = 32)
	private String operator;

	@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
	@Temporal(TemporalType.DATE)
	private Date etdzDate;

	@Column(length = 2)
	private String payMode;


	private Double insurance;

	/**
	 * 是否已付款
	 * 0/null: 未付款
	 * 1: 已付款
	 */
	@Column(name = "BEPAYED", length = 1)
	private String bePaid;

	/**
	 * 使用状态
	 */
	@Column(name = "USEDSTATUS", length = 1)
	private String useStatus;

	private String dcity; // length = 3 ?

	private String acity; // length = 3 ?

	/**
	 * 是否已审核
	 */
	@Column(name = "BEAUDITED", length = 1)
	private String audited;

	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name="create_time", insertable = false, updatable = false)
	private Date createTime;

	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "last_update", insertable = false)
	private Date lastUpdate;

	/**
	 * 该票号包含的行程信息
	 */
	@Transient
	private List<BillFlightTicketDetail> details;

	@Transient
	private List<FlightInfo> flights;


	public String getFlightNo() {
		return getFlights().size() == 0 ? "" : getFlights().get(0).getFlightNo();
	}

	public String getSubclass() {
		return getFlights().size() == 0 ? "" : getFlights().get(0).getSubclass();
	}

	public String getDport() {
		return getFlights().size() == 0 ? "" : getFlights().get(0).getDepartureAirport();
	}

	public String getDportName() {
		return getFlights().size() == 0 ? "" : getFlights().get(0).getDepartureAirportName();
	}

	public String getAport() {
		return getFlights().size() == 0 ? "" : getFlights().get(0).getArrivalAirport();
	}

	public String getAportName() {
		return getFlights().size() == 0 ? "" : getFlights().get(0).getArrivalAirportName();
	}

	public String getDterm() {
		return getFlights().size() == 0 ? "" : getFlights().get(0).getDepartureTerminal();
	}

	public String getAterm() {
		return getFlights().size() == 0 ? "" : getFlights().get(0).getArrivalTerminal();
	}

	public String getDdate() {
		return getFlights().size() == 0 ? "" : getFlights().get(0).getDepartureDate();
	}

	public String getDtime() {
		return getFlights().size() == 0 ? "" : getFlights().get(0).getDepartureTime();
	}

	public String getAtime() {
		return getFlights().size() == 0 ? "" : getFlights().get(0).getArrivalTime();
	}


	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}


	public String getTicketNo() {
		return ticketNo;
	}

	public void setTicketNo(String ticketNo) {
		if (ticketNo.length() == 13) {
			this.ticketNo = ticketNo.substring(3);
			this.balanceCode = ticketNo.substring(0, 3);
		} else if (ticketNo.length() == 14) {
      this.ticketNo = ticketNo.substring(4);
      this.balanceCode = ticketNo.substring(0, 3);
    } else if (ticketNo.length() == 17) {
      this.balanceCode = ticketNo.substring(0, 3);
      this.ticketNo = ticketNo.substring(4);
		} else {
			this.ticketNo = ticketNo;
		}
	}

	public String getTicketClass() {
		return ticketClass;
	}

	public void setTicketClass(String ticketClass) {
		this.ticketClass = ticketClass;
	}

	public String getPassengerName() {
		return passengerName;
	}

	public void setPassengerName(String passengerName) {
		this.passengerName = passengerName;
	}

	public Date getSaleDate() {
		return saleDate;
	}

	public void setSaleDate(Date saleDate) {
		this.saleDate = saleDate;
	}

	public double getAccountRecv() {
		return accountRecv;
	}

	public void setAccountRecv(double accountRecv) {
		this.accountRecv = accountRecv;
	}

	public String getPnrNo() {
		return pnrNo == null ? "" : pnrNo.trim();
	}

	public void setPnrNo(String pnrNo) {
		this.pnrNo = pnrNo;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public double getParvalue() {
		return parvalue;
	}

	public void setParvalue(double parvalue) {
		this.parvalue = parvalue;
	}

	public double getTax() {
		return tax;
	}

	public void setTax(double tax) {
		this.tax = tax;
	}

	public double getCustomerCommissionRate() {
		return customerCommissionRate;
	}

	public void setCustomerCommissionRate(double customerCommissionRate) {
		this.customerCommissionRate = customerCommissionRate;
	}

	public double getDiscount() {
		return discount;
	}

	public void setDiscount(double discount) {
		this.discount = discount;
	}

	public double getDiscount2() {
		return discount2;
	}

	public void setDiscount2(double discount2) {
		this.discount2 = discount2;
	}

	public double getHcharge() {
		return hcharge;
	}

	public void setHcharge(double hcharge) {
		this.hcharge = hcharge;
	}

	public double getServiceCharge() {
		return serviceCharge;
	}

	public void setServiceCharge(double serviceCharge) {
		this.serviceCharge = serviceCharge;
	}

	public double getLowFare() {
		return lowFare;
	}

	public void setLowFare(double lowFare) {
		this.lowFare = lowFare;
	}

	/**
	 * @return the airportTax
	 */
	public double getAirportTax() {
		return airportTax;
	}

	/**
	 * @param airportTax the airportTax to set
	 */
	public void setAirportTax(double airportTax) {
		this.airportTax = airportTax;
	}

	/**
	 * @return the fuelSurcharge
	 */
	public double getFuelSurcharge() {
		return fuelSurcharge;
	}

	/**
	 * @param fuelSurcharge the fuelSurcharge to set
	 */
	public void setFuelSurcharge(double fuelSurcharge) {
		this.fuelSurcharge = fuelSurcharge;
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	/**
	 * @return the enterpriseId
	 */
	public int getEnterpriseId() {
		return enterpriseId;
	}

	/**
	 * @param enterpriseId the enterpriseId to set
	 */
	public void setEnterpriseId(int enterpriseId) {
		this.enterpriseId = enterpriseId;
	}

	public String getBalanceCode() {
		return balanceCode;
	}

	public void setBalanceCode(String balanceCode) {
		this.balanceCode = balanceCode;
	}

	public String getBillNum() {
		return billNum.trim();
	}

	public void setBillNum(String billNum) {
		this.billNum = billNum;
	}

	public String getRouteFlightNo() {
		if (routeFlightNo == null || routeFlightNo.trim().length() == 0) {
			if (this.details != null && this.details.size() > 0) {
				return this.details.get(0).getFlight();
			} else {
				return null;
			}
		} else {
			return routeFlightNo;
		}
	}

	public void setRouteFlightNo(String routeFlightNo) {
		this.routeFlightNo = routeFlightNo;
	}

	public String getRouteDate() {
		if (routeDate == null || routeDate.trim().length() == 0) {
			if (this.details != null && this.details.size() > 0) {
				return this.details.get(0).getDdate();
			} else {
				return null;
			}
		} else {
			return routeDate;
		}
	}



	public void setRouteDate(String routeDate) {
		this.routeDate = routeDate;
	}

	public String getRouteSegInfo() {
		if (routeSegInfo == null) {
      return "";
    } else {
      return routeSegInfo;
    }
	}

	public void setRouteSegInfo(String routeSegInfo) {
		this.routeSegInfo = routeSegInfo;
	}

	public String getRouteDetail() {
		if (routeDetail == null) {
      return "";
    } else {
      return routeDetail;
    }
	}

	public void setRouteDetail(String routeDetail) {
		this.routeDetail = routeDetail;
	}

	public String getOperator() {
		return operator;
	}

	public void setOperator(String operator) {
		this.operator = operator;
	}

	public Date getEtdzDate() {
		return etdzDate;
	}

	public void setEtdzDate(Date etdzDate) {
		this.etdzDate = etdzDate;
	}

	public String getPayMode() {
		return payMode;
	}

	public void setPayMode(String payMode) {
		this.payMode = payMode;
	}

	public double getInsurance() {
		return insurance == null ? 0 : insurance;
	}

	public void setInsurance(double insurance) {
		this.insurance = insurance;
	}

	public String getBePaid() {
		return bePaid;
	}

	public void setBePaid(String bePaid) {
		this.bePaid = bePaid;
	}

	public String getUseStatus() {
		return useStatus == null ? "" : useStatus;
	}

	public void setUseStatus(String useStatus) {
		this.useStatus = useStatus;
	}

	public String getDcity() {
		if (dcity == null || dcity.trim().length() == 0) {
			if (this.details != null && this.details.size() > 0) {
				return this.details.get(0).getDport() + this.details.get(0).getDportName();
			} else {
				return null;
			}
		} else {
			return dcity;
		}
	}

	public void setDcity(String dcity) {
		this.dcity = dcity;
	}

	public String getAcity() {
		if (acity == null || acity.trim().length() == 0) {
			if (this.details != null && this.details.size() > 0) {
				return this.details.get(0).getAport() + this.details.get(0).getAportName();
			} else {
				return null;
			}
		} else {
			return acity;
		}
	}

	public void setAcity(String acity) {
		this.acity = acity;
	}

	public String getRouteClass() {
		if (routeClass == null || routeClass.trim().length() == 0) {
			if (this.details != null && this.details.size() > 0) {
				return this.details.get(0).getSubclass();
			} else {
				return null;
			}
		} else {
			return routeClass;
		}

	}

	public void setRouteClass(String routeClass) {
		this.routeClass = routeClass;
	}

	public String getAudited() {
		return audited;
	}

	public void setAudited(String audited) {
		this.audited = audited;
	}

	public String getPayMemo() {
		return payMemo;
	}

	public void setPayMemo(String payMemo) {
		this.payMemo = payMemo;
	}

	public List<BillFlightTicketDetail> getDetails() {
		if (details == null) {
      details = new ArrayList<>();
    }
		return details;
	}

	public void setDetails(List<BillFlightTicketDetail> details) {
		this.details = details;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public Date getLastUpdate() {
		return lastUpdate;
	}

	public void setLastUpdate(Date lastUpdate) {
		this.lastUpdate = lastUpdate;
	}

	public Integer getTicketCount() {
		return ticketCount == null ? 1 : ticketCount;
	}

	public void setTicketCount(Integer ticketCount) {
		this.ticketCount = ticketCount;
	}

	public String getTicketNoEnd() {
		return ticketNoEnd;
	}

	public void setTicketNoEnd(String ticketNoEnd) {
		this.ticketNoEnd = ticketNoEnd;
	}

	public Integer getCustomerId() {
		return customerId;
	}

	public void setCustomerId(Integer customerId) {
		this.customerId = customerId;
	}

	public List<FlightInfo> getFlights() {
		if (flights == null) {
      flights = new ArrayList<>();
    }
		return flights;
	}

	public void setFlights(List<FlightInfo> flights) {
		this.flights = flights;
	}

	public Date getOrderDate() {
		return orderDate;
	}

	public void setOrderDate(Date orderDate) {
		this.orderDate = orderDate;
	}
}
